//logs.js
// import * as echarts from '../../ec-canvas/echarts';
const app = getApp();
var loginInfo = {};

function linkGo(obj, to, json) {
  var query = "", i = 0;
  if (json) {
    for (var key in json) {
      query += i === 0 ? '?' : '&';
      i++;
      query += key + '=' + json[key];
    }
  }

  to != 'redirectTo' ? wx.navigateTo({
    url: '../' + obj + '/' + obj + query
  }) : wx.redirectTo({
    url: '../' + obj + '/' + obj + query
  });
}
//test 111111
function api() {
  var api = {
    img: 'https://dhh5.joylinksgroup.com/static/image/xcx',
    // api: 'https://dhapi.joylinksgroup.com',
    // web: 'https://dhh5.joylinksgroup.com',
    // api: 'https://test.dhapi.joylinksgroup.com',
    web: 'https://test.dhh5.joylinksgroup.com',
    api: 'https://dhapi.joylinksgroup.com', // 王家大院
    //api: 'https://test.dhapi.joylinksgroup.com' // 敦煌

  }
  return api
  // return 'http://test.dhapi.joylinksgroup.com'
}

function ajax(obj) {
  const method = obj.method || 'post',
    header = obj.method || {
      'content-type': 'application/x-www-form-urlencoded'
    }
  wx.request({
    url: api().api + obj.url,
    method,
    header,
    data: obj.data,
    success(res) {
      obj.success(res);
      wx.showLoading({
        title: '加载中',
        mask:true
      })
    },
    error(res) {
      obj.error(res);
    },
    complete(res){
      wx.hideLoading({
        success: (res) => {},
      })
    }
  })
}

function pushLoinInfo(msg) {
  loginInfo = msg;
}
function toast(title, icon) {
  wx.showToast({
    title: title,
    icon: icon || '',
    duration: 2000,
    mask: true
  })
}
function share() {
  return {
    title: '审核',
    path: "/pages/index/index",
    imageUrl: '../../images/share.jpg'
  }
}
function getLogin(){
  return new Promise(function (resolve, reject) {
    wx.login({
      success: res => {
        if (res.errMsg.indexOf('ok') > 0) {
          app.globalData.code = res.code;
          // 获取用户信息
          wx.getSetting({
            success: res => {
              if (res.authSetting['scope.userInfo']) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                wx.getUserInfo({
                  success: res => {
                    // 可以将 res 发送给后台解码出 unionId
                    app.globalData.userInfo = res.userInfo;
                    var userInfo = {
                      code: app.globalData.code,
                      ...app.globalData.userInfo
                    }
                    ajax({
                      data: userInfo,
                      url: '/user/login',
                      success: (res) => {
                        resolve(res);
                        // if (res.data.code == 200) {
                        //   app.globalData.mobile = res.data.data.mobile;
                        //   app.globalData.openId = res.data.data.openid;
                        //   app.globalData.session_key = res.data.data.session_key;
                        //   if (res.data.data.result_code){
                            
                        //   } else {
                        //     setTimeout(()=>{
                        //     linkGo('purchase', 'redirectTo');
                        //   },50)
                        //   }
                        // }
                      }
                    })
                    // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                    // 所以此处加入 callback 以防止这种情况
                    // if (this.userInfoReadyCallback) {
                    //   this.userInfoReadyCallback(res)
                    // }
                  }
                })
              } else {
                // linkGo('purchase', 'redirectTo');
                reject(res);
              }
            },
            error:res=>{
              reject(res);
            }
          });
        } else {
          toast(res.errMsg);
          reject(res);
        }
      },
      error:res=>{
        reject(res);
      }
    })
  })
}

exports.apiDomain = api();
exports.linkGo = linkGo;
exports.ajax = ajax;
exports.commLoginInfo = loginInfo;
exports.pushLoinInfo = pushLoinInfo;
exports.toast = toast;
exports.share = share();
exports.getLogin = getLogin;

//exports.getLoginInfo = getLoginInfo;